<template>
  <div class="popup-styles-tab">
    <!-- 搜索和操作区域 -->
    <div class="tab-header">
      <div class="search-area">
        <el-form :inline="true" :model="searchForm" class="search-form">
          <el-form-item label="站点">
            <el-select v-model="searchForm.siteId" placeholder="全部站点" style="width: 140px;" clearable>
              <el-option 
                v-for="site in sites" 
                :key="site.id" 
                :label="site.name" 
                :value="site.id" 
              />
            </el-select>
          </el-form-item>
          <el-form-item label="位置">
            <el-select v-model="searchForm.position" placeholder="全部位置" style="width: 140px;" clearable>
              <el-option label="左上角" value="top_left" />
              <el-option label="顶部居中" value="top_center" />
              <el-option label="右上角" value="top_right" />
              <el-option label="居中" value="center" />
              <el-option label="左下角" value="bottom_left" />
              <el-option label="底部居中" value="bottom_center" />
              <el-option label="右下角" value="bottom_right" />
            </el-select>
          </el-form-item>
          <el-form-item label="动画">
            <el-select v-model="searchForm.animation" placeholder="全部动画" style="width: 140px;" clearable>
              <el-option label="淡入" value="fade" />
              <el-option label="滑动" value="slide" />
              <el-option label="缩放" value="zoom" />
              <el-option label="弹跳" value="bounce" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="action-area">
        <el-button type="primary" @click="$emit('create', 'add')">
          <el-icon><Plus /></el-icon>
          新增样式
        </el-button>
        <el-button @click="$emit('refresh')">
          <el-icon><Refresh /></el-icon>
          刷新
        </el-button>
      </div>
    </div>

    <!-- 样式列表 -->
    <div class="styles-list">
      <el-table 
        :data="filteredStyles" 
        border 
        style="width: 100%"
        v-loading="loading"
        empty-text="暂无样式数据"
      >
        <el-table-column prop="name" label="样式名称" min-width="150" />
        <el-table-column prop="siteName" label="站点" width="120" />
        <el-table-column prop="position" label="位置" width="120">
          <template #default="scope">
            <el-tag :type="getPositionTagType(scope.row.position)">
              {{ getPositionText(scope.row.position) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="尺寸" width="120">
          <template #default="scope">
            <div class="size-info">
              <div>{{ scope.row.width }} × {{ scope.row.height }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="样式预览" width="200">
          <template #default="scope">
            <div class="style-preview" :style="getPreviewStyle(scope.row)">
              <div class="preview-content">
                <div class="preview-header">预览</div>
                <div class="preview-body">弹窗内容</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="animation" label="动画" width="100">
          <template #default="scope">
            <el-tag :type="getAnimationTagType(scope.row.animation)">
              {{ getAnimationText(scope.row.animation) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="创建时间" width="160" />
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="scope">
            <el-button size="small" @click="$emit('edit', scope.row)">编辑</el-button>
            <el-button 
              size="small" 
              type="success" 
              @click="handlePreview(scope.row)"
            >预览</el-button>
            <el-button 
              size="small" 
              type="danger" 
              @click="handleDelete(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination-area">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { Plus, Refresh } from '@element-plus/icons-vue'
import type { Site, PopupStyle } from '@/types/sitePopup'

// Props
interface Props {
  sites: Site[]
  styles: PopupStyle[]
  loading: boolean
}

const props = defineProps<Props>()

// Emits
const emit = defineEmits<{
  refresh: []
  create: ['add' | 'edit']
  edit: [PopupStyle]
  delete: [string]
  preview: [string, string]
}>()

// 响应式数据
const searchForm = ref({
  siteId: '',
  position: '',
  animation: ''
})

const currentPage = ref(1)
const pageSize = ref(20)
const total = ref(0)

// 计算属性
const filteredStyles = computed(() => {
  let result = [...props.styles]
  
  if (searchForm.value.siteId) {
    result = result.filter(item => item.siteId === searchForm.value.siteId)
  }
  
  if (searchForm.value.position) {
    result = result.filter(item => item.position === searchForm.value.position)
  }
  
  if (searchForm.value.animation) {
    result = result.filter(item => item.animation === searchForm.value.animation)
  }
  
  total.value = result.length
  
  // 分页处理
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return result.slice(start, end)
})

// 监听样式数据变化
watch(() => props.styles, () => {
  total.value = props.styles.length
  currentPage.value = 1
}, { immediate: true })

// 方法
const handleSearch = () => {
  currentPage.value = 1
}

const resetSearch = () => {
  searchForm.value = {
    siteId: '',
    position: '',
    animation: ''
  }
  currentPage.value = 1
}

const handleDelete = (id: string) => {
  emit('delete', id)
}

const handlePreview = (style: PopupStyle) => {
  emit('preview', style.siteId, style.id)
}

const handleSizeChange = (size: number) => {
  pageSize.value = size
  currentPage.value = 1
}

const handleCurrentChange = (page: number) => {
  currentPage.value = page
}

const getPositionTagType = (position: string) => {
  const positionMap: Record<string, string> = {
    'top_left': 'info',
    'top_center': 'success',
    'top_right': 'warning',
    'center': 'primary',
    'bottom_left': 'info',
    'bottom_center': 'success',
    'bottom_right': 'warning'
  }
  return positionMap[position] || 'info'
}

const getPositionText = (position: string) => {
  const positionMap: Record<string, string> = {
    'top_left': '左上角',
    'top_center': '顶部居中',
    'top_right': '右上角',
    'center': '居中',
    'bottom_left': '左下角',
    'bottom_center': '底部居中',
    'bottom_right': '右下角'
  }
  return positionMap[position] || position
}

const getAnimationTagType = (animation: string) => {
  const animationMap: Record<string, string> = {
    'fade': 'info',
    'slide': 'success',
    'zoom': 'warning',
    'bounce': 'danger'
  }
  return animationMap[animation] || 'info'
}

const getAnimationText = (animation: string) => {
  const animationMap: Record<string, string> = {
    'fade': '淡入',
    'slide': '滑动',
    'zoom': '缩放',
    'bounce': '弹跳'
  }
  return animationMap[animation] || animation
}

const getPreviewStyle = (style: PopupStyle) => {
  return {
    width: `${style.width / 4}px`,
    height: `${style.height / 4}px`,
    backgroundColor: style.backgroundColor,
    border: `${style.borderWidth}px solid ${style.borderColor}`,
    borderRadius: `${style.borderRadius}px`,
    boxShadow: style.shadow ? '0 2px 8px rgba(0,0,0,0.15)' : 'none',
    position: 'relative',
    overflow: 'hidden'
  }
}
</script>

<style scoped>
.popup-styles-tab {
  padding: 20px;
}

.tab-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
  gap: 20px;
}

.search-area {
  flex: 1;
}

.search-form {
  margin: 0;
}

.action-area {
  display: flex;
  gap: 12px;
  flex-shrink: 0;
}

.styles-list {
  margin-bottom: 20px;
}

.pagination-area {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.size-info {
  font-size: 12px;
  text-align: center;
}

.style-preview {
  display: flex;
  justify-content: center;
  align-items: center;
}

.preview-content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.preview-header {
  background-color: #f0f0f0;
  padding: 2px 4px;
  font-size: 8px;
  text-align: center;
  border-bottom: 1px solid #ddd;
}

.preview-body {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 8px;
  color: #666;
}

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table th) {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: 600;
}

:deep(.el-table td) {
  padding: 12px 0;
}

:deep(.el-button--small) {
  padding: 6px 12px;
  font-size: 12px;
}
</style>
